<?php

namespace App\Http\Controllers\Admin\Ad;

use App\Http\Controllers\Controller;
use App\Http\Requests\Advert\StoreAdvert;
use App\Http\Requests\Advert\UpdateAdvert;

use App\Models\Ad\Advert;
use App\Models\Ad\Category;
use Illuminate\Http\Request;

class AdvertController extends Controller
{
    public function __construct()
    {
        view()->share([
            'categories' => Category::orderBy('sort_order','desc')->get(),

            '_ads'=>'am-in',
            '_advert'=>'am-active',
        ]);
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        //模糊搜索
        $where = function ($query) use ($request) {
            if ($request->has('keyword') and $request->keyword != '') {
                $search = "%" . $request->keyword . "%";
                $query->where('title', 'like', $search); //title跟表里字段要一直
            }
        };

        $adverts = Advert::with('category')->where($where)->orderBy('sort_order', 'desc')->paginate(config('admin.page_size'));
        return view('admin.ad.advert.index', compact('adverts'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('admin.ad.advert.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     */
    public function store(StoreAdvert $request)
    {
        Advert::create($request->all());
        return redirect(route('ad.advert.index'))->with('success', '新增广告成功！');
    }

    /**
     * Display the specified resource.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $advert = Advert::find($id);
        return view('admin.ad.advert.edit', compact('advert'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function update(UpdateAdvert $request, $id)
    {
        $advert = Advert::find($id);
        $advert->update($request->all());
        return redirect(route('ad.advert.index'))->with('success', '编辑广告成功！！！');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        Advert::destroy($id);
    }

    /***
     * 多选删除
     */
    public function delete_all(Request $request)
    {
        Advert::destroy($request->check_id);
    }

    /**
     * 排序
     */
    public function sort_order(Request $request)
    {
        $advert = Advert::find($request->id);
        $advert->sort_order = $request->sort_order;
        $advert->save();
    }

    /**
     * 改变属性
     */
    public function change_attr(Request $request)
    {
        $advert = Advert::find($request->id);
        $attr = $request->attr;
        $advert->$attr = !$request->$attr;
        $advert->save();
    }
}
